package com.sjx.mumu

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import com.sjx.lib_base.BaseActivity
import com.sjx.lib_base.adapter.BasePagerAdapter
import com.sjx.lib_base.manager.ARouterManager
import com.sjx.mumu.data.MainListData
import com.sjx.mumu.databinding.ActivityMainBinding
import com.zhy.magicviewpager.transformer.ScaleInTransformer

/**
 * FileName: MainActivity
 * Founder: sjx
 * Profile: 主Activity
 */
class MainActivity : BaseActivity<ActivityMainBinding>() {

    private val mList = ArrayList<MainListData>()
    private val mListView = ArrayList<View>()

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun isShowBack(): Boolean {
        return false
    }

    override fun getTitleText(): String {
        return "app"
    }

    override fun initView() {
        initPagerData()
        initPagerView()
    }

    @SuppressLint("Recycle")
    private fun initPagerData() {
        val title = resources.getStringArray(com.sjx.lib_base.R.array.MainTitleArray)
        val color = resources.getIntArray(R.array.MainColorArray)
        val icon = resources.obtainTypedArray(R.array.MainIconArray)

        for ((index, value) in title.withIndex()) {
            mList.add(MainListData(value, icon.getResourceId(index, 0), color[index]))
        }

        //非调试版本去除工程模式
        if (!BuildConfig.DEBUG) {
            mList.removeAt(mList.size - 1)
        }

        val windowHeight = windowManager.defaultDisplay.height
        mList.forEach {
            val view = View.inflate(this, R.layout.layout_main_list, null)
            val mCvMainView = view.findViewById<CardView>(R.id.mCvMainView)
            val mIvMainIcon = view.findViewById<ImageView>(R.id.mIvMainIcon)
            val mTvMainText = view.findViewById<TextView>(R.id.mTvMainText)
            mCvMainView.setBackgroundColor(it.color)
            mIvMainIcon.setImageResource(it.icon)
            mTvMainText.text = it.title

            mCvMainView.layoutParams?.let { lp ->
                lp.height = windowHeight / 5 * 3
            }

            //点击事件
            view.setOnClickListener { _ ->
                when (it.icon) {
                    R.drawable.img_main_weather -> ARouterManager.startActivity(ARouterManager.PATH_WEATHER)
                    else -> {
                        //todo 待定
                    }
                }
            }

            mListView.add(view)
        }
    }

    private fun initPagerView() {
        with(binding.mViewPager) {
            pageMargin = 20
            offscreenPageLimit = mList.size
            BasePagerAdapter(mListView)
            setPageTransformer(true, ScaleInTransformer())
        }
    }

}